package top.lenjon.lib.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import top.lenjon.lib.entity.Admin;
import top.lenjon.lib.entity.UserDetail;
import top.lenjon.lib.mapper.AdminMapper;
import top.lenjon.lib.mapper.UserDetailMapper;
import top.lenjon.lib.service.IAdminService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;
import java.util.Optional;

/**
 * <p>
 * 管理员 服务实现类
 * </p>
 *
 * @author wei
 * @since 2020-11-09
 */
@Service
public class AdminServiceImpl extends ServiceImpl<AdminMapper, Admin> implements IAdminService {

    @Autowired
    AdminMapper adminMapper;
    @Autowired
    UserDetailMapper userDetailMapper;

    @Override
    public boolean resetPassword(int id, int type) {
        switch (type) {
            case 1:
                return adminMapper.resetAdminPswById(id) == 1;
            case 0:
                return adminMapper.resetUserPswById(id) == 1;
            default:
                return false;
        }
    }

    @Override
    public boolean addAdmin(Admin admin) {
        return this.save(admin);
    }

    @Override
    public boolean addUser(UserDetail userDetail) {
        return userDetailMapper.insert(userDetail) == 1;
    }

    @Override
    public boolean deleteAdmin(int id) {
        return Optional.ofNullable(adminMapper.selectById(id))
                .map(a -> {
                    a.setEnabled(false);
                    a.setUpdateTime(LocalDateTime.now());
                    adminMapper.updateById(a);
                    return true;
                }).orElse(false);
    }

    @Override
    public boolean deleteUser(int id) {
        return Optional.ofNullable(userDetailMapper.selectById(id))
                .map(u -> {
                    u.setEnabled(false);
                    u.setUpdateTime(LocalDateTime.now());
                    userDetailMapper.updateById(u);
                    return true;
                }).orElse(false);
    }

    @Override
    public PageInfo getListByType(int type, int pageNum) {
        PageHelper.startPage(pageNum, 1000);
        switch (type) {
            case 1:
                List<Admin> adminList = adminMapper.getAll();
                adminList.forEach(a -> a.setPassword("****"));
                return new PageInfo<>(adminList);
            case 0:
                List<UserDetail> userDetailList = userDetailMapper.getAll();
                userDetailList.forEach(a -> a.setPassword("****"));
                return new PageInfo<>(userDetailList);
            default:
                return null;
        }
    }

    @Override
    public Admin getAdminByUsername(String username) {
        return adminMapper.getAdminByName(username);
    }


}
